Skip to main content

Average of Multiple Matrices

This is an example of computing the average of a multiple matrix inputs.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleMultipleMatrix(Model):
    def define(self):        n = 3        m = 6
        # Declare a matrix of shape 3x6 as input        M1 = self.declare_variable(            'M1',            val=np.arange(n * m).reshape((n, m)),        )
        # Declare another matrix of shape 3x6 as input        M2 = self.declare_variable(            'M2',            val=np.arange(n * m, 2 * n * m).reshape((n, m)),        )
        # Output the elementwise average of matrices M1 and M2        self.register_output(            'multiple_matrix_average',            csdl.average(M1, M2),        )

sim = Simulator(ExampleMultipleMatrix())sim.run()
print('M1', sim['M1'].shape)print(sim['M1'])print('M2', sim['M2'].shape)print(sim['M2'])print('multiple_matrix_average', sim['multiple_matrix_average'].shape)print(sim['multiple_matrix_average'])
[[ 0.  1.  2.  3.  4.  5.] [ 6.  7.  8.  9. 10. 11.] [12. 13. 14. 15. 16. 17.]]M2 (3, 6)[[18. 19. 20. 21. 22. 23.] [24. 25. 26. 27. 28. 29.] [30. 31. 32. 33. 34. 35.]]multiple_matrix_average (3, 6)[[ 9. 10. 11. 12. 13. 14.] [15. 16. 17. 18. 19. 20.] [21. 22. 23. 24. 25. 26.]]